// import create from 'zustand'

import { StateCreator } from 'zustand';
import { AppStore } from '.';

/**
 * 存储全局动作状态,比如拖拽中。
 */
interface ActionData {
  addFileInfo: {
    // 是否打开新建文件
    addFileType?: 'file' | 'folder' | 'rename';
    // 是否打开新建文件
    isOpenAddFile?: boolean;
    // 新建文件名称
    newFileName?: string;
    // 新建文件的路径
    newFilePath?: string;
  }
  // 是否显示set框
  setVisible: boolean;
  leftCurrentTab: string;
  bottomCurrentTab: string;
  newContractVisible: boolean;
  newnewContractVisible: string;
  editDeployContractsVisible: boolean;
  editCompileContractsVisible: boolean;
  editHiddenContractsVisible: boolean;
  modalStatus: {
    modalShow: boolean; modalContent: string
  },
  expandedIds: string[]
}

const initData = {
  addFileInfo: {
    addFileType: undefined,
    isOpenAddFile: false,
    newFileName: '',
    newFilePath: ''
  },
  setVisible: false,
  leftCurrentTab: 'fileTree',
  bottomCurrentTab: 'output',
  newContractVisible: false,
  newnewContractVisible: '',
  expandedIds: [],
  editDeployContractsVisible: false,
  editCompileContractsVisible: false,
  editHiddenContractsVisible: false,
  modalStatus: {
    modalShow: false,
    modalContent: ''
  }
};

export type ActionDataStore = ActionData & {
  // setDragType: (type?: string) => void;
  setAddFileInfo: (info?: ActionData['addFileInfo']) => void;
  // setIsHideNav: (isHide?: boolean) => void;
  // setIsHideFunc: (isHide?: boolean) => void;
  // setIsHideBottom: (visible: boolean) => void;
  setModalVisible: (visible: boolean) => void;
  setLeftCurrentTab: (tab: string) => void;
  setBottomCurrentTab: (tab: string) => void;
  setModalStatus: (statue: ActionData['modalStatus']) => void;
  setNewContractVisible: (visible: boolean) => void;
  setNewnewContractVisible: (visible: string) => void;
  setEditDeployContractsVisible: (visible: boolean) => void;
  setEditCompileContractsVisible: (visible: boolean) => void;
  setEditHiddenContractsVisible: (visible: boolean) => void;
  setExpandedIds: (ids: string[]) => void;
  contactExpandedIds: (ids: string[]) => void;
};
export const actionStore: StateCreator<ActionDataStore> = (set,get) => ({
  ...initData,
  setAddFileInfo: (info) => set((state) => ({
    addFileInfo: {
      ...state.addFileInfo,
      ...info
    }
  })),
  setModalVisible: (visible) => set(() => ({
    setVisible: visible
  })),
  setLeftCurrentTab: (tab) => set(() => ({
    leftCurrentTab: tab
  })),
  setBottomCurrentTab: (tab) => set(() => ({
    bottomCurrentTab: tab
  })),
  setModalStatus: (status) => set(() => ({
    modalStatus: status
  })),
  setNewnewContractVisible: (visible) => set(() => ({
    newnewContractVisible: visible
  })),
  setNewContractVisible: (visible) => set(() => ({
    newContractVisible: visible
  })),
  setEditDeployContractsVisible: (visible) => set(() => ({
    editDeployContractsVisible: visible
  })),
  setEditCompileContractsVisible: (visible) => set(() => ({
    editCompileContractsVisible: visible
  })),
  setEditHiddenContractsVisible: (visible) => set(() => ({
    editHiddenContractsVisible: visible
  })),
  setExpandedIds: (ids:string[]) => set(() => ({
    expandedIds: ids
  })),
  contactExpandedIds: (ids:string[]) => {
    const stat = get() as AppStore;
    const expandedIds = stat.expandedIds;
    ids.forEach((path)=>{
      if(!expandedIds.includes(path)){
        expandedIds.push(path);
      }
    })
    set({
      expandedIds:[...expandedIds]
    })
  }
});
